<?php
/**
 * 首页精选接口
 */
//require_once dirname(__DIR__)."/common.php";
//$uuid=3;
require_once "login_filter.php";

use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\cls\tom\Tools;
use app\model\HomePage;

$uuid = $Auth['uuid'];


//分页
$page = isset($_REQUEST["page"]) ? intval($_REQUEST["page"]) : 1;
$page = $page > 0 ? $page : 1;
$pageSize = 30;
//最小id
$small_selection = isset($_REQUEST["small_selection"]) ? intval($_REQUEST["small_selection"]) : ""; // 精选
$small_ordinary = isset($_REQUEST["small_ordinary"]) ? intval($_REQUEST["small_ordinary"]) : ""; // 普通
//实例化数据库
$db = SysResource::getDB(false);
$Home_page = new HomePage($db);


//如果page为1的时候就要去查询三次user_material表，大于1就只要查询两次表
if ($page == 1) {
    // 没有LAST_ID
    $time = times();
    // $jingxuan = one_v1($Home_page, $page, $pageSize, $time);

    $topArr = $Home_page->quality(); // 置顶数据
    $normalArr = $Home_page->quality1($time, $page, $pageSize, ""); // 常规数据
    empty($normalArr) ? null : shuffle($normalArr);
    $jingxuan = array_merge($topArr, $normalArr);
} else if ($page > 1 && !empty($small_selection) || !empty($small_ordinary)) {
    // 有LAST_ID
    $time = times();
    // $jingxuan = two_v1($Home_page, $page, $pageSize, $small_selection, $small_ordinary, $time);

    $jingxuan = $Home_page->quality1($time, $page, $pageSize, $small_selection); // 常规数据

} else {
    Response::output(-1, "参数异常");
}


// 追加其他数据

// 先去redis里查询用户关注和点赞了哪些视频
$redis = SysResource::getRedis();
$uids = sprintf("%d", crc32($uuid));
$key1 = REDIS_PREFIX . "fabulous_" . $uids;
$key2 = REDIS_PREFIX . "follow_" . $uids;
//$da1=$redis->HKEYS($key1);//点赞
//$da2=$redis->HKEYS($key2);//关注
$da1 = "";
$da2 = "";
$zans = [];
$zan_id = [];
$guanzus = [];
if (empty($da1)) {
    //数据库查询已点赞视频
    $da1 = $Home_page->zan($uuid);
    foreach ($da1 as $k => $v) {
        $zans[] = $v['video_uuid'];
        $zan_id[] = $v['other_id'];
    }

} else {
    $zans = $da1;
}
if (empty($da2)) {
    //数据库查询已关注视频
    $da2 = $Home_page->guanzu($uuid);
    foreach ($da2 as $kk => $vv) {
        $guanzus[] = $vv['other_uuid'];
    }
} else {
    $guanzus = $da2;
}
// 去除空数组
$jingxuan = array_filter($jingxuan);
$jing = [];
if (!empty($jingxuan)) {

    foreach ($jingxuan as $r => $t) {
        $jing[] = $t;
    }
    foreach ($jing as $kkk => &$vvv) {
        if (in_array($vvv['video_uuid'], $zans) && in_array($vvv['id'], $zan_id)) {
            $vvv['is_zan'] = 2;
        } else {
            $vvv['is_zan'] = 1;
        }
        if (in_array($vvv['uuid'], $guanzus)) {
            $vvv['is_follow'] = 2;
        } else {
            $vvv['is_follow'] = 1;
        }
        if ($vvv['uuid'] == $uuid) {
            $vvv['is_follow'] = 3;
        }
        if ($vvv['uuid'] == $uuid) {
            $vvv['is_user'] = 1;
        } else {
            $vvv['is_user'] = 0;
        }
        $vvv['count_material_love_10'] = round($vvv['count_material_love'] * 1.7);
        $vvv['count_material_share_10'] = round($vvv['count_material_share'] * 1.7);
        if (empty($vvv['share_pic'])) {
            $vvv['share_pic'] = "";
        }
        if (empty($vvv['video_desc'])) {
            $vvv['video_desc'] = "";
        }
    }
} else {
    $jing = [];
}
$length = count($jing);
if ($length < $pageSize) {
    Response::output(0, "查询成功", $jing, $length);
} else {
    Response::output(0, "查询成功", $jing, $length);
}

// ------------------------------------------------------------------------

function one($Home_page, $page, $pageSize, $time)
{
    $Home_data = [];
    //先去查询有没有精选排序的
    $quality1 = $Home_page->quality();

    //判断精选排序是否为空
    if (empty($quality1)) {
        //如果为空，就取51条精选视频
        $quality2 = $Home_page->quality1($time, $page, $pageSize, "");
        //再判quality是否为空,打乱数组顺序
        if (!empty($quality2)) {
            shuffle($quality2);
        }

    } else {

        //如果不为空，就取53条精选视频
        $quality2 = $Home_page->quality1($time, $page, $pageSize, "");
        //再判quality是否为空
        if (!empty($quality2)) {
            shuffle($quality2);
        }
    }

    //再去取普通列表是精选三倍
    $ordinary = $Home_page->ordinary($time, $page, $pageSize * 3, "");
    if (!empty($ordinary)) {
        shuffle($ordinary);
    }

    $length = count($quality1);
    if ($length == 1) {
        $Home_data[] = $quality1[0];
        $Home_data[] = $quality2[0];
        unset($quality2[0]);
    } else if ($length == 2) {
        $Home_data[] = $quality1[0];
        $Home_data[] = $quality1[1];
    } else {
        $Home_data[] = $quality2[0];
        $Home_data[] = $quality2[1];
        unset($quality2[0]);
        unset($quality2[1]);
    }


    //重精选数组中取1条，普通取三条，如果精选或者普通为空就不循环了
    for ($x = 0; $x < 52; $x++) {
        if (!empty($ordinary)) {
            $key2 = array_rand($ordinary, 1);
            $Home_data[] = $ordinary[$key2];
            unset($ordinary[$key2]);
            $key3 = array_rand($ordinary, 1);
            $Home_data[] = $ordinary[$key3];
            unset($ordinary[$key3]);
            $key4 = array_rand($ordinary, 1);
            $Home_data[] = $ordinary[$key4];
            unset($ordinary[$key4]);
        }
        if (!empty($quality2)) {
            $key1 = array_rand($quality2, 1);
            $Home_data[] = $quality2[$key1];
            unset($quality2[$key1]);
        }
    }
    return $Home_data;
}

function two($Home_page, $page, $pageSize, $small_selection, $small_ordinary, $time)
{
    //取50条精选视频
    $quality2 = $Home_page->quality1($time, $page, $pageSize, $small_selection);
    //再判quality2是否为空
    if (!empty($quality2)) {
        shuffle($quality2);
    }
    //再去取普通列表是精选三倍,150条
    $ordinary = $Home_page->ordinary($time, $page, $pageSize * 3, $small_ordinary);
    if (!empty($ordinary)) {
        shuffle($ordinary);
    }
    //重精选数组中取1条，普通取三条，如果精选或者普通为空就不循环了
    for ($x = 0; $x < 51; $x++) {
        if (!empty($quality2)) {
            $key1 = array_rand($quality2, 1);
            $Home_data[] = $quality2[$key1];
            unset($quality2[$key1]);
        }
        if (!empty($ordinary)) {
            $key2 = array_rand($ordinary, 1);
            $Home_data[] = $ordinary[$key2];
            unset($ordinary[$key2]);
            $key3 = array_rand($ordinary, 1);
            $Home_data[] = $ordinary[$key3];
            unset($ordinary[$key3]);
            $key4 = array_rand($ordinary, 1);
            $Home_data[] = $ordinary[$key4];
            unset($ordinary[$key4]);
        }
    }
    //$quality2,$ordinary，$Home_data
    return $Home_data;
}

//按本周时间查询数据，拼接的时间
function times()
{
    $today = strtotime(date("Y-m-d"), time());
    $time['end'] = date("Y-m-d H:i:s", $today + 86400);
    $time['start'] = date("Y-m-d H:i:s", $today - 6 * 86400);
    return $time;
}

